package com.zj.leetcode.medium;

/**
 * @program: algorithm
 * @description: 神奇字符串
 * 1 22 11 2 1 22 1 22 11 2 11 22 ......
 * 1 2  2  1 1 2  1 2  2  1 2  2 ......
 * 481 magical-string
 * @author: Zhang Bo
 * @create: 2022-01-14 17:05
 **/
public class MagicalString {
    public static void main(String[] args) {
        Solution solution = new MagicalString().new Solution();
        System.out.println(solution.magicalString(6));
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public int magicalString(int n) {
            int[] s = new int[n];
            s[0] = 1;
            for (int i = 1, j = 1; i < n; j++) {
                s[i] = (s[i - 1] & 1) + 1;
                i++;
                if (s[j] == 2 && i < n) {
                    s[i] = s[i - 1];
                    i++;
                }
            }
            int res = 0;
            for (int c : s) {
                res += c & 1;
            }
            return res;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}